package com.zghl.controller;

import com.zghl.pojo.Equipment;
import com.zghl.service.EquipmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/equipment")
public class EquipmentController {
    @Resource
    private EquipmentService equipmentService;


    @RequestMapping("/getList")
    public ModelAndView getList(){
        List<Equipment> equipment = equipmentService.EquipmentList(null);

        ModelAndView modelAndView=new ModelAndView();

        modelAndView.setViewName("equipment");
        modelAndView.addObject("list",equipment);

        return modelAndView;
    }

    @RequestMapping("/add")
    public ModelAndView add(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("equipmentAdd");

        Equipment equipment=new Equipment();
        modelAndView.addObject("equ",equipment);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/save")
    public ModelAndView addEquipment(@ModelAttribute Equipment equipment){
        Integer integer = equipmentService.AddByEquipment(equipment);
        if (integer>0){
           return getList();
        }
        throw  new RuntimeException("添加错误");
    }

    @RequestMapping("/delete/{EquipmentId}")
    public ModelAndView deleteEquipment(@PathVariable("EquipmentId") int EquipmentId){
        Integer integer = equipmentService.deleteByEquipment(EquipmentId);

        if (integer>0){
            return getList();
        }
        throw  new RuntimeException("删除错误");
    }


    @RequestMapping("/update/{EquipmentId}")
    public ModelAndView update(@PathVariable("EquipmentId") int EquipmentId){

        Map<String,Object> map=new HashMap<>();

        map.put("id",EquipmentId);

        List<Equipment> equipment1 = equipmentService.EquipmentList(map);

        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("equipmentUpdate");

        Equipment equipment=new Equipment();

        for (Equipment ad:equipment1) {
            BeanUtils.copyProperties(ad,equipment);
        }
        modelAndView.addObject("upequ",equipment);
        modelAndView.addObject("EquipmentId",EquipmentId);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/updateSvae/{EquipmentId}")
    public ModelAndView updateEquipment(@PathVariable("EquipmentId") int EquipmentId,@ModelAttribute Equipment equipment){
        Equipment equipment1=new Equipment();

        BeanUtils.copyProperties(equipment,equipment1);
        equipment1.setId(EquipmentId);

        Integer integer = equipmentService.updateByEquipment(equipment1);
        System.out.println(equipment1.toString());
        if (integer>0){
            return getList();
        }
        throw  new RuntimeException("添加错误");
    }
}
